from django.shortcuts import render,redirect
from django.http import HttpResponse,JsonResponse
from django.core.urlresolvers import reverse
from .. import models
from . import cate_views,user_views
import os
from shop.settings import BASE_DIR
from django.core.paginator import Paginator

def addgoods(request):
	types = cate_views.tab()
	print(types,'666')
	return render(request,'myadmin/goods/addgoods.html',{'types':types})



def goodsinsert(request):
	ginfo = request.POST.dict()
	ginfo.pop('csrfmiddlewaretoken')
	print(ginfo)

	file = request.FILES.get('g_url')
	if not file:
		return HttpResponse('<script>alert("请选择图片");history.back(-1)</script>')


	# 调图片上传
	g_url=user_views.upload(file)

	# 入库
	goods = models.Goods()
	goods.title=ginfo['title']
	goods.ordernum=ginfo['ordernum']
	goods.g_url=g_url
	goods.price=ginfo['price']
	goods.ginfo=ginfo['ginfo']
	goods.cateid=models.Cates.objects.get(id=ginfo['cateid'])
	goods.save()

	# 返回
	return redirect(reverse('myadmin_listgoods'))


# 列表
def listgoods(request):
	goods = models.Goods.objects.all()

	types = request.GET.get('type')
	# 接受关键字
	search = request.GET.get('search')
	# 判断用是否搜索内容
	if types:
		if types == 'all':
			from django.db.models import Q
			goods = models.Goods.objects.filter(Q(id__contains=search)|Q(title__contains=search)|Q(price__contains=search))
		elif types=='title':
			goods = models.Goods.objects.filter(title__contains=search)
		elif types=='price':
			goods = models.Goods.objects.filter(price__contains=search)
		elif types == 'id':
			goods = models.Goods.objects.filter(id__contains=search)

		# 实例化分页对象	
	p = Paginator(goods, 5)

	#一共可以分多少页
	sumpage=p.num_pages

	# 取第几页的数据
	# 接受用户的页码	
	page = int(request.GET.get('p',1))
	# 第几页的数据
	page1 = p.page(page)

	# 判断 如果用输入的页码<=3 显示前五个页码
	if page<=3:
	# 页码的迭代序列  [1,2,3,4,5,6,7]
		prange = p.page_range[:5]
	elif page+2>=sumpage:
		prange = p.page_range[-5:]
	else:
		prange = p.page_range[page-3:page+2]



	# 进行数据分配 通过 模板语法将数据展示到模板当中
	return render(request,'myadmin/goods/listgoods.html',{'goods':page1,'prange':prange,'page':page,'sumpage':sumpage})

# 删除
def delgoods(request):
	uid = request.GET.get('uid')
	goods = models.Goods.objects.get(id=uid)
	goods.delete()
	return redirect(reverse('myadmin_listgoods'))
# 修改
def editgoods(request):
	types = cate_views.tab()
	uid = request.GET.get('uid')
	print(uid)
	if request.method == 'GET':
		goods = models.Goods.objects.get(id=uid)
		return render(request,'myadmin/goods/editgoods.html',{'goods':goods,'types':types})
	elif request.method == 'POST':
		newgo = models.Goods.objects.get(id=uid)

		edit = request.POST.dict()
		edit.pop('csrfmiddlewaretoken')
		print(edit,"+++++++++")
		# 更新数据
		
		newgo.title=edit['title']
		newgo.price=edit['price']
		newgo.ordernum=edit['ordernum']
		newgo.ginfo=edit['ginfo']

		file = request.FILES.get('g_url')
		print(file,'_______')
		if file:
			os.remove('.'+newgo.g_url)
			headurl=user_views.upload(file)
			newgo.g_url=headurl

		newgo.save()
		
		return redirect(reverse('myadmin_listgoods'))
